package com.yami.shop.api.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yami.shop.bean.model.MeltCloudUserRemark;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.security.api.model.YamiUser;
import com.yami.shop.security.api.util.SecurityUtils;
import com.yami.shop.service.MeltCloudUserRemarkService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("/p/user/or/group/remark")
@Tag(name = "用户/群备注接口")
@AllArgsConstructor
public class UserRemarkController {
    
    private final MeltCloudUserRemarkService meltCloudUserRemarkService;

    @PostMapping("/set")
    @Operation(summary =  "设置备注")
    public ServerResponseEntity setRemark(@RequestBody @Valid MeltCloudUserRemark remark) {
        YamiUser user = SecurityUtils.getUser();
        String userId = user.getUserId();
        remark.setUserId(userId);
        MeltCloudUserRemark one = meltCloudUserRemarkService.getOne(Wrappers.lambdaQuery(MeltCloudUserRemark.class)
                .eq(MeltCloudUserRemark::getUserId, userId).eq(MeltCloudUserRemark::getType, remark.getType()).eq(MeltCloudUserRemark::getObjId, remark.getObjId()));
        if (one == null) {
            meltCloudUserRemarkService.save(remark);
        } else {
            one.setRemark(remark.getRemark());
            meltCloudUserRemarkService.updateById(one);
        }
        return ServerResponseEntity.success();
    }
    
    
    
    
    
    
}
